package com.example.thecharacterbook;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

public class TheCheck extends Fragment {
    private FloatingActionButton AddBtn;
    private MyDataBase db;
    ArrayList<String> word_id, word, mean, example, example2, example3;
    MyAdapter_copyforFragment myAdapter;
    RecyclerView recyclerView;
    private Context context;
    private Activity activity;
    @Override

    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_main, container, false);
        context= view.getContext();
        AddBtn = view.findViewById(R.id.Add);
        recyclerView = view.findViewById(R.id.Rv);
        AddBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, TheAddMethod.class);
                startActivityForResult(intent, 1);
            }
        });
        db = new MyDataBase(context);
        word_id = new ArrayList<>();
        word = new ArrayList<>();
        mean = new ArrayList<>();
        example = new ArrayList<>();
        example2 = new ArrayList<>();
        example3 = new ArrayList<>();
        Log.v("word_id", "ok");
        Log.v("word_id", "IS" + word_id);
        displayData();
        myAdapter = new MyAdapter_copyforFragment(getActivity(), context, word_id, word, mean, example, example2, example3);
        Log.v("word_id", "IS" + word_id);
        Log.v("word", "IS" + word);
        recyclerView.setAdapter(myAdapter);
        DividerItemDecoration dI = new DividerItemDecoration(context ,DividerItemDecoration.VERTICAL);
        recyclerView.addItemDecoration(dI);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        return view;
    }
    void displayData() {
        Cursor cursor = db.readAllData();
        System.out.println(cursor);
        if (cursor.getCount() == 0) {
            Toast.makeText(context, "Input Error!", Toast.LENGTH_SHORT).show();
        } else {
            while (cursor.moveToNext()) {
                word_id.add(cursor.getString(0));
                word.add(cursor.getString(1));
                mean.add(cursor.getString(2));
                example.add(cursor.getString(3));
                example2.add(cursor.getString(4));
                example3.add(cursor.getString(5));
            }
        }
    }

    public void onCreateOptionsMenu(Menu menu) {
        getActivity().getMenuInflater().inflate(R.menu.search,menu);
        MenuItem item = menu.findItem(R.id.Menu_Search1);
        SearchView searchView = (SearchView) item.getActionView();
        if (searchView.equals(null)){

        }
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                myAdapter.getFilter().filter(newText);
                return false;
            }
        });

    }
}